package com.mentaleye.biz.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.TeacherBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.TeacherBiz;
import com.mentaleye.dal.model.Dictionary;
import com.mentaleye.dal.model.TeacherExample;
import com.mentaleye.dal.model.UserLogin;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.service.TeacherService;
import com.mentaleye.service.UserLoginService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Service
public class TeacherBizImpl implements TeacherBiz {


	@Autowired private DictionaryBiz dictionaryBiz;
	
	@Autowired
	private TeacherService teacherService;
	
	@Autowired private UserLoginService loginService;
	
	private static Logger logger = Logger.getLogger(TeacherBizImpl.class);

	@Override
	public CommonResult editTeacher(TeacherBean teacherBean) {
		CommonResult result = new CommonResult();
		String defaultPassword = getDefaultPassword();
		boolean add;
		boolean success = false;
		add=teacherBean.getTeacherNo()==null;
		try {
			success = teacherService.editTeacher(teacherBean, add);
			if(add)
			{
				addTeacherToLogin(teacherBean, defaultPassword);
			}
		} catch (Exception e) {
			logger.error("教工编辑异常", e);
			success = false;
		}
		result.setIsSuccess(success);
		return result;
	}
	/**
	 * 添加教师到登录表
	 * @param teacherBean
	 * @param defaultPassword
	 */
	public void addTeacherToLogin(TeacherBean teacherBean,String defaultPassword){
		UserLogin data=new UserLogin();
		data.setLoginId(teacherBean.getJobNumber());
		data.setLoginName(teacherBean.getTeacherName());
		data.setPassword(defaultPassword);
		data.setCreateTime(new Date());
		data.setUpdateTime(new Date());
		data.setUserType(DictionaryEnums.TEACHER_TYPE.getCode());
		this.loginService.insertRecord(data);
	}
	/**
	 * 得到默认密码
	 * @return
	 */
	public String getDefaultPassword(){
		String defaultPassword=null;
		List<Dictionary> dictionaryList = this.dictionaryBiz.getListByParent(DictionaryEnums.DEFAULT_PASSWORD.getCode());
		if(dictionaryList.isEmpty()){
			defaultPassword="888888";
		}
		else
		{
			defaultPassword=dictionaryList.get(0).getDictionaryValue();
		}
		return defaultPassword;
	}

	@Override
	public CommonResult deleteTeacher(List<Integer> ids) {
		CommonResult commonResult=null;
		try {
			commonResult=new CommonResult();
			if(this.loginService.deleteByKeyList(ids)){
				commonResult.setIsSuccess(teacherService.deleteTeacherByList(ids));
			}
			else
			{
				commonResult.setResultMsg("删除失败！");
			}
		} catch (Exception e) {
			logger.error("教工删除异常", e);
			commonResult.setResultMsg("教工删除异常");
		}
		 return commonResult;
	}

	@Override
	public List<TeacherBean> queryAllTeacher() {
		try {
			List<TeacherBean> list = teacherService.getAllTeachers(new TeacherExample());
			return list;
		} catch (Exception e) {
			logger.error("教工全部查询异常", e);
			return null;
		}

	}

	@Override
	public List<TeacherBean> queryTeacherByName(String teacherName) {
		try {
			List<TeacherBean> list = teacherService.getTeacherByName(teacherName);
			return list;
		} catch (Exception e) {
			logger.error("根据名字查询教师列表异常", e);
			return null;
		}

	}

	@Override
	public TeacherBean queryTeacherByNo(int teacherNo) {
		try {
			TeacherBean teacherBean = teacherService.getTeacherByNo(teacherNo);
			return teacherBean;
		} catch (Exception e) {
			logger.error("根据编号查询教师异常", e);
			return null;
		}

	}

	@Override
	public CommonResult editStatus(int teacherStatus) {
		try {
			TeacherBean teacherBean = new TeacherBean();
			teacherBean.setTeacherStatus(teacherStatus);
			return new CommonResult(teacherService.editStatus(teacherBean));
		} catch (Exception e) {
			logger.error("编辑教师状态异常", e);
			return new CommonResult();
		}

	}

	@Override
	public void queryTeacherByPage(CommonPage commonPage) {
		try {
			teacherService.queryTeacherByPage(commonPage);
		} catch (Exception e) {
			logger.error("分页查询教师异常", e);
		}

	}

	@Override
	public List<TeacherBean> queryTeacherByIdList(List<Integer> ids) {
		try {
			List<TeacherBean> list = teacherService.getTeacherListByIds(ids);
			return list;
		} catch (Exception e) {
			logger.error("根据教师编号查询教师列表异常", e);
			return null;
		}

	}
	
	@Override
	public void queryTeacherByDept(CommonPage commonPage) {
		try {
			teacherService.queryTeacherByDept(commonPage);
		} catch (Exception e) {
			logger.error("部门教师查询异常", e);
		}

	}

	@Override
	public List<TeacherBean> queryTeacherByNotOaClass(Integer personRole) {
		try {
			return teacherService.queryTeacherByNotOaClass(personRole);
		} catch (Exception e) {
			logger.error("根据没有班级查询教师列表异常", e);
			return null;
		}

	}

	@Override
	public List<TeacherBean> queryTeacherByNotInIdList(List<Integer> ids) {
		try {
			List<TeacherBean> list = teacherService.getTeacherListByNotInIds(ids);
			return list;
		} catch (Exception e) {
			logger.error("查询所选编号之外的教师列表异常", e);
			return null;
		}

	}
	
	@Override
	public List<TeacherBean> queryTeacher(){
		try {
			return teacherService.queryTeacher();
		} catch (Exception e) {
			logger.error("查询教师异常", e);
			return null;
		}
		
	}

	@Override
	public void queryRoleTeacherByPage(CommonPage commonPage) {
		try {
			teacherService.queryRoleTeacherByPage(commonPage);
		} catch (Exception e) {
			logger.error("分页查询教师异常", e);
		}
	}

	@Override
	public List<TeacherBean> queryAllTeacherByRoleNo(Integer id) {
		try {
			return teacherService.queryAllTeacherByRoleNo(id);
		} catch (Exception e) {
			logger.error("角色条件查询教师异常", e);
			return null;
		}
	}

	@Override
	public List<TeacherBean> queryAllTeacherNoDel() {
		try {
			return teacherService.queryTeacherNoDel();
		} catch (Exception e) {
			logger.error("查询教师未删除异常", e);
			return null;
		}
	}
	@Override
	public CommonResult checkByTeacherBean(TeacherBean teacherBean) {
		CommonResult commonResult=new CommonResult();
		TeacherExample teacherExample = new TeacherExample();
		List<TeacherBean> list = null;
		try {
			if(teacherBean.getJobNumber() != null){
				teacherExample.createCriteria().andjobNumberEqualTo(teacherBean.getJobNumber());
				list = teacherService.getTeacherByExample(teacherExample);
				commonResult = setCommonResult(list, "jobNumber");
			}else if(teacherBean.getTeacherIdCard() !=null){
				teacherExample.createCriteria().andTeacherIdCardEqualTo(teacherBean.getTeacherIdCard());
				list = teacherService.getTeacherByExample(teacherExample);
				commonResult = setCommonResult(list, "teacherIdCard");
			}else if(teacherBean.getTeacherTelephone() !=null){
				teacherExample.createCriteria().andTeacherTelephoneEqualTo(teacherBean.getTeacherTelephone());
				list = teacherService.getTeacherByExample(teacherExample);
				commonResult = setCommonResult(list, "teacherTelephone");
			}
		} catch (Exception e) {
			logger.error("教工删除异常", e);
		}
		 return commonResult;
	}
	
	public CommonResult setCommonResult(List<TeacherBean> list,String data){
		CommonResult commonResult=new CommonResult();
		if(list!=null && list.size()>0){
			commonResult.setData(data);
			commonResult.setIsSuccess(false);
		}else{
			commonResult.setData(data);
			commonResult.setIsSuccess(true);
		}
		return commonResult;
	}

}
